package com.example.pinkcat.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.pinkcat.R;
import com.example.pinkcat.adapter.Dongtai_RecyclerViewAdapter;
import com.example.pinkcat.api.getData.getData_waibu;

public class Fragment_dongtai extends Fragment implements SwipeRefreshLayout.OnRefreshListener{

    Context context;

    private RecyclerView recyclerView;
    private Dongtai_RecyclerViewAdapter dongtaiAdapter;

    private SwipeRefreshLayout swipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dongtai,container,false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        getData_waibu.getActivity(context,dongtaiAdapter);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        context=view.getContext();
        recyclerView = view.findViewById(R.id.dongtai_RV);
        recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(),1));

        swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.dt_srl);//初始
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.pink,R.color.purple_200);

        dongtaiAdapter = new Dongtai_RecyclerViewAdapter(view.getContext());
        recyclerView.setAdapter(dongtaiAdapter);


    }


    //下拉刷新
    public void onRefresh(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("fragment","下拉刷新");
                int pageNumer = 0;;
                PostData(); //加载数据
                swipeRefreshLayout.setRefreshing(false);
            }
        },1000);
    }


    public void PostData(){
        getData_waibu.getActivity(context,dongtaiAdapter);
    }

}
